﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using zdzh.zdmgr.BLL;
using zdzh.zdmgr.COL;
using zdzh.zdmgr.SFL;

namespace zdzh.zdmgr.UIL.dingdanp
{
    /// <summary>
    /// 对象名称：“回迁协议模块”修改信息页面（用户界面层）
    /// 对象说明：该页面用于修改“回迁协议”对象并更新至数据库，操作完成后返回列表页面。
    /// 作者姓名：飞翔熊
    /// 编写日期：2012/12/3 13:23:16
    /// </summary>
    public partial class Modify : System.Web.UI.Page
    {
        private Dingdan dingdan;
        protected Fangyuan fangyuan;
        List<Huiqianhu> huiqianhu;
        List<Huiqiandianhua> dianhua;
        List<Yuangong> yuangong;

        /// <summary>
        /// “回迁协议模块”修改信息页面载入时的事件处理方法。
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            dingdan = DingdanBLL.GetDataByIndex(Request.QueryString["Index"]); // 从数据库中获取关联对象信息，以备进行修改操作。
            if (dingdan == null) 
                throw new CustomException("对不起，所要修改的信息不存在或已删除。");
            fangyuan = FangyuanBLL.GetDataByIndex(dingdan.FangyuanIndex);
            huiqianhu = HuiqianhuBLL.GetDataByXieyihao(dingdan.Index);
            dianhua = HuiqiandianhuaBLL.GetDataByXieyihao(dingdan.Index);

            if (!Page.IsPostBack)
            {
                yuangong = YuangongBLL.GetListByType(3);
                DdlSeller.Items.Add(new ListItem("无", "0"));
                BindObjectToForm();  // 将关联对象当前值，绑定至界面控件进行显示。
                DdlPaymentMode_SelectedIndexChanged(null, null);
            }
        }

        /// <summary>
        /// 用户单击“保存”按钮时的事件处理方法。
        /// </summary>
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            BindFormlToObject();
            DingdanBLL.Update(dingdan);
            fangyuan.Price = dingdan.Danjia;
            FangyuanBLL.Update(fangyuan);
            Main.SysMessage.ShowSuccessMsg("“定单”修改成功，单击“确定”返回信息列表。", "Default.aspx");
        }

        #region 界面控件与关联对象之间的绑定方法

        /// <summary>
        /// 将界面控件中的值，绑定给关联对象。
        /// </summary>
        private void BindFormlToObject()
        {
            //if (!DataValid.IsNullOrInt(TxtFangyuanIndex.Text))
            //    throw new CustomException("“房源索引”不是一个有效的整数，请您重新输入。");

            if (!DataValid.IsNullOrInt(TxtNumber.Text))
                throw new CustomException("“编号”不是一个有效的整数，请您重新输入。");

            if (!DataValid.IsNullOrDecimal(TxtShoufu.Text))
                throw new CustomException("“首付”不是一个有效的整数，请您重新输入。");

            if (!DataValid.IsNullOrDecimal(TxtDaikuan.Text))
                throw new CustomException("“贷款年限”不是一个有效的整数，请您重新输入。");

            if (!DataValid.IsNullOrInt(TxtDingjin.Text))
                throw new CustomException("“定金”不是一个有效的整数，请您重新输入。");

            if (!DataValid.IsNullOrDateTime(TxtBukuanDate.Text))
                throw new CustomException("“补款日期”不是一个有效格式的日期，正确格式应为“" + DateTime.Now.ToString("yyyy-MM-dd") + "”，请您重新输入。");

            if (!DataValid.IsNullOrDecimal(TxtDanjia.Text))
                throw new CustomException("“单价”不是一个有效的整数，请您重新输入。");

            if (!DataValid.IsNullOrInt(TxtZongjia.Text))
                throw new CustomException("“总价”不是一个有效的整数，请您重新输入。");

            if (!DataValid.IsNullOrDateTime(TxtDutDate.Text))
                throw new CustomException("“认购日期”不是一个有效格式的日期，正确格式应为“" + DateTime.Now.ToString("yyyy-MM-dd") + "”，请您重新输入。");
            
            dingdan.DutDate = DataValid.GetNullOrDateTime(TxtDutDate.Text);
            dingdan.Number = TxtNumber.Text;
            dingdan.PaymentMode = DdlPaymentMode.SelectedIndex;
            dingdan.Danjia = DataValid.GetNullOrDecimal(TxtDanjia.Text);
            dingdan.Zongjia = DataValid.GetNullOrDecimal(TxtZongjia.Text);
            dingdan.PaymentMode = DdlPaymentMode.SelectedIndex;
            dingdan.Dingjin = DataValid.GetNullOrDecimal(TxtDingjin.Text);
            dingdan.Shoufu = DataValid.GetNullOrDecimal(TxtShoufu.Text);
            dingdan.Daikuan = DataValid.GetNullOrDecimal(TxtDaikuan.Text);
            dingdan.BukuanDate = DataValid.GetNullOrDateTime(TxtBukuanDate.Text);
            dingdan.SellerIndex = DataValid.GetNullOrInt(DdlSeller.SelectedItem.Value);
            dingdan.Beizhu = TxtBeizhu.Text;
        }

        /// <summary>
        /// 将关联对象中的值，绑定至界面控件进行显示。
        /// </summary>
        private void BindObjectToForm()
        {
            TxtDutDate.Text = dingdan.DutDate.Value.ToShortDateString();
            TxtNumber.Text = dingdan.Number;
            foreach (Huiqianhu model in huiqianhu)
            {
                if (TxtName.Text.Length > 0)
                    TxtName.Text += "、";
                TxtName.Text += model.Xingming;

                if (model.IDNumber != null && model.IDNumber.Length > 0)
                {
                    if (TxtIDNumber.Text.Length > 0)
                        TxtIDNumber.Text += "、";
                    TxtIDNumber.Text += model.IDNumber;
                }
            }
            foreach(Huiqiandianhua model in dianhua)
            {
                if (TxtPhone.Text.Length > 0)
                    TxtPhone.Text += "、";
                TxtPhone.Text += model.Dianhua;
            }
            TxtBuild.Text = fangyuan.getBuild() + " " + fangyuan.getType();
            LblArea.Text = fangyuan.Area.ToString() + "㎡";
            TxtDanjia.Text = dingdan.Danjia.ToString();
            TxtZongjia.Text = dingdan.Zongjia.ToString();
            DdlPaymentMode.SelectedIndex = dingdan.PaymentMode.Value;
            TxtDingjin.Text = dingdan.Dingjin.ToString();
            TxtShoufu.Text = dingdan.Shoufu.ToString();
            TxtDaikuan.Text = dingdan.Daikuan.ToString();
            if (dingdan.BukuanDate.HasValue)
            {
                TxtBukuanDate.Text = dingdan.BukuanDate.Value.ToShortDateString();
            }
            int sellerindex = 0;
            foreach (Yuangong model in yuangong)
            {
                if (dingdan.SellerIndex == model.Index)
                    sellerindex = DdlSeller.Items.Count;
                DdlSeller.Items.Add(new ListItem(model.Name, model.Index.ToString()));
            }
            DdlSeller.SelectedIndex = sellerindex;
            TxtBeizhu.Text = dingdan.Beizhu;
        }

        #endregion 界面控件与关联对象之间的绑定方法

        protected void DdlPaymentMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (DdlPaymentMode.SelectedIndex)
            {
                case 0:
                    PnlFenqi.Visible = false;
                    PnlDaikuan.Visible = false;
                    break;
                case 1:
                    PnlFenqi.Visible = true;
                    PnlDaikuan.Visible = false;
                    break;
                case 2:
                    PnlFenqi.Visible = false;
                    PnlDaikuan.Visible = true;
                    TxtDaikuan.Focus();
                    break;
            }
        }

        protected void TxtDanjia_TextChanged(object sender, EventArgs e)
        {
            if (TxtDanjia.Text == "")
            {
                TxtZongjia.Text = null;
                return;
            }
            decimal? value = Convert.ToDecimal(TxtDanjia.Text) * fangyuan.Area;
            //decimal zongjia=DataValid.Round(Convert.ToDecimal(value), 0);
            decimal zongjia = Math.Round(Convert.ToDecimal(value), 0, MidpointRounding.AwayFromZero);
            TxtZongjia.Text = Convert.ToString(Convert.ToInt32(zongjia));
            TxtDingjin.Focus();
        }

        protected void CalBukuan_SelectionChanged(object sender, EventArgs e)
        {
            TxtBukuanDate.Text = CalBukuan.SelectedDate.Date.ToShortDateString();
            CalBukuan.Visible = false;
        }

        protected void BtnCalendar_Click(object sender, ImageClickEventArgs e)
        {
            CalBukuan.Visible = !CalBukuan.Visible;
        }

        protected void TxtFenqi_TextChanged(object sender, EventArgs e)
        {
            LblHuankuan.Text = "还款 " + ((Convert.ToDecimal(TxtZongjia.Text) - Convert.ToDecimal(TxtDingjin.Text)) / Convert.ToInt32(TxtFenqi.Text)).ToString() + "月";
            TxtBeizhu.Text = "月还款 " + ((Convert.ToDecimal(TxtZongjia.Text) - Convert.ToDecimal(TxtDingjin.Text)) / Convert.ToInt32(TxtFenqi.Text)).ToString() + "元";
        }

        protected void TxtShoufu_TextChanged(object sender, EventArgs e)
        {
            if (TxtShoufu.Text == "")
            {
                TxtDaikuan.Text = null;
                return;
            }
            TxtDaikuan.Text = (Convert.ToDecimal(TxtZongjia.Text) - Convert.ToDecimal(TxtShoufu.Text)).ToString();
        }

        protected void TxtDaikuan_TextChanged(object sender, EventArgs e)
        {
            if (TxtDaikuan.Text == "")
            {
                TxtShoufu.Text = null;
                return;
            }
            TxtShoufu.Text = (Convert.ToDecimal(TxtZongjia.Text) - Convert.ToDecimal(TxtDaikuan.Text)).ToString();

            TxtBukuanDate.Focus();
        }


    }
}
